import { getSongs } from '../../request/api.js'
export default {
  namespaced: true,
  state() {
    return {
      urlList: [],
      musicTotal: 0,
      musicCurrent: 0,
      musicState: true,

      musicCurrentId: '', //当前播放音乐id
      musicIdList: [], //播放列表
      musicSrc: '', //播放地址
      musicDetail: {}, //歌曲信息
      musicLyric: '', //歌词
    }
  },

  mutations: {
    setUrlList(state, urlList) {
      state.urlList = urlList
    },
    setmusicTotal(state, musicTotal) {
      state.musicTotal = musicTotal
    },
    setmusicCurrent(state, musicCurrent) {
      state.musicCurrent = musicCurrent
    },
    setmusicState(state, musicState) {
      state.musicState = musicState
    },
    setmusicDeail(state, musicDeail) {
      state.musicDeail = musicDeail
    },

    setMusicCurrentId(state, { currentId, isAdd }) {
      if (isAdd) {
        if (Array.isArray(currentId)) {
          state.musicCurrentId = currentId[0]
          state.musicIdList = currentId
        } else {
          if (state.musicIdList.length == 0) {
            state.musicIdList = [currentId]
          } else {
            let list = [...state.musicIdList]
            list.splice(
              list.findIndex(i => i == state.musicCurrentId),
              0,
              currentId
            )
            state.musicIdList = list
          }
          state.musicCurrentId = currentId
        }
      }

      state.musicCurrentId = Array.isArray(currentId) ? currentId[0] : currentId
    },

    setMusicIdList(state, musicIdList) {
      state.musicIdList = musicIdList
    },

    setMusicSrc(state, musicSrc) {
      state.musicSrc = musicSrc
    },

    setMusicDetail(state, musicDetail) {
      state.musicDetail = musicDetail
    },

    setMusicLyric(state, musicLyric) {
      state.musicLyric = musicLyric
    },
  },

  actions: {
    setUrlListAction({ commit }, urlList) {
      let list = []
      urlList.forEach(id => {
        list.push(getSongs.getUrl({ id }))
      })

      Promise.all(list).then(res => {
        commit(
          'setUrlList',
          res.map(item => item.data[0])
        )
      })
    },

    setMusicCurrentIdAction({ commit, state }, { currentId, num }) {
      let musicCurrentId2
      let isAdd = true

      if (typeof num != 'undefined') {
        isAdd = false
        const { musicCurrentId, musicIdList } = state
        const i = musicIdList.findIndex(j => j == musicCurrentId) + num
        if (i == musicIdList.length) {
          musicCurrentId2 = musicIdList[0]
        } else if (i == -1) {
          musicCurrentId2 = musicIdList[musicIdList.length - 1]
        } else {
          musicCurrentId2 = musicIdList[i]
        }
      }

      currentId = musicCurrentId2 ?? currentId

      commit('setMusicCurrentId', { currentId, isAdd })

      const id = Array.isArray(currentId) ? currentId[0] : currentId
      getSongs.getUrl({ id }).then(res => commit('setMusicSrc', res.data[0]))
      getSongs.deail({ ids: id }).then(res => commit('setMusicDetail', res.songs[0]))
      getSongs.getLyric({ id }).then(res => commit('setMusicLyric', res.lrc.lyric))
    },
  },
}
